package com.whiteape.book.model.gen;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.whiteape.book.model.dto.MediaChapterBean;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "MEDIA_CHAPTER_BEAN".
*/
public class MediaChapterBeanDao extends AbstractDao<MediaChapterBean, String> {

    public static final String TABLENAME = "MEDIA_CHAPTER_BEAN";

    /**
     * Properties of entity MediaChapterBean.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, String.class, "id", true, "ID");
        public final static Property Link = new Property(1, String.class, "link", false, "LINK");
        public final static Property Name = new Property(2, String.class, "name", false, "NAME");
        public final static Property TaskName = new Property(3, String.class, "taskName", false, "TASK_NAME");
        public final static Property DownloadStatus = new Property(4, int.class, "downloadStatus", false, "DOWNLOAD_STATUS");
        public final static Property MediaId = new Property(5, String.class, "mediaId", false, "MEDIA_ID");
        public final static Property Start = new Property(6, Long.class, "start", false, "START");
        public final static Property End = new Property(7, Long.class, "end", false, "END");
    }

    private Query<MediaChapterBean> collectionMediaBean_MediaChapterListQuery;
    private Query<MediaChapterBean> downloadTaskBean_MediaChapterListQuery;

    public MediaChapterBeanDao(DaoConfig config) {
        super(config);
    }
    
    public MediaChapterBeanDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"MEDIA_CHAPTER_BEAN\" (" + //
                "\"ID\" TEXT PRIMARY KEY NOT NULL ," + // 0: id
                "\"LINK\" TEXT," + // 1: link
                "\"NAME\" TEXT," + // 2: name
                "\"TASK_NAME\" TEXT," + // 3: taskName
                "\"DOWNLOAD_STATUS\" INTEGER NOT NULL ," + // 4: downloadStatus
                "\"MEDIA_ID\" TEXT," + // 5: mediaId
                "\"START\" INTEGER," + // 6: start
                "\"END\" INTEGER);"); // 7: end
        // Add Indexes
        db.execSQL("CREATE INDEX " + constraint + "IDX_MEDIA_CHAPTER_BEAN_MEDIA_ID ON \"MEDIA_CHAPTER_BEAN\"" +
                " (\"MEDIA_ID\" ASC);");
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"MEDIA_CHAPTER_BEAN\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, MediaChapterBean entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
 
        String link = entity.getLink();
        if (link != null) {
            stmt.bindString(2, link);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(3, name);
        }
 
        String taskName = entity.getTaskName();
        if (taskName != null) {
            stmt.bindString(4, taskName);
        }
        stmt.bindLong(5, entity.getDownloadStatus());
 
        String mediaId = entity.getMediaId();
        if (mediaId != null) {
            stmt.bindString(6, mediaId);
        }
 
        Long start = entity.getStart();
        if (start != null) {
            stmt.bindLong(7, start);
        }
 
        Long end = entity.getEnd();
        if (end != null) {
            stmt.bindLong(8, end);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, MediaChapterBean entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
 
        String link = entity.getLink();
        if (link != null) {
            stmt.bindString(2, link);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(3, name);
        }
 
        String taskName = entity.getTaskName();
        if (taskName != null) {
            stmt.bindString(4, taskName);
        }
        stmt.bindLong(5, entity.getDownloadStatus());
 
        String mediaId = entity.getMediaId();
        if (mediaId != null) {
            stmt.bindString(6, mediaId);
        }
 
        Long start = entity.getStart();
        if (start != null) {
            stmt.bindLong(7, start);
        }
 
        Long end = entity.getEnd();
        if (end != null) {
            stmt.bindLong(8, end);
        }
    }

    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
    }    

    @Override
    public MediaChapterBean readEntity(Cursor cursor, int offset) {
        MediaChapterBean entity = new MediaChapterBean( //
            cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // link
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // name
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // taskName
            cursor.getInt(offset + 4), // downloadStatus
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // mediaId
            cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6), // start
            cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7) // end
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, MediaChapterBean entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
        entity.setLink(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setTaskName(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setDownloadStatus(cursor.getInt(offset + 4));
        entity.setMediaId(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setStart(cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6));
        entity.setEnd(cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7));
     }
    
    @Override
    protected final String updateKeyAfterInsert(MediaChapterBean entity, long rowId) {
        return entity.getId();
    }
    
    @Override
    public String getKey(MediaChapterBean entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(MediaChapterBean entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "mediaChapterList" to-many relationship of CollectionMediaBean. */
    public List<MediaChapterBean> _queryCollectionMediaBean_MediaChapterList(String mediaId) {
        synchronized (this) {
            if (collectionMediaBean_MediaChapterListQuery == null) {
                QueryBuilder<MediaChapterBean> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.MediaId.eq(null));
                collectionMediaBean_MediaChapterListQuery = queryBuilder.build();
            }
        }
        Query<MediaChapterBean> query = collectionMediaBean_MediaChapterListQuery.forCurrentThread();
        query.setParameter(0, mediaId);
        return query.list();
    }

    /** Internal query to resolve the "mediaChapterList" to-many relationship of DownloadTaskBean. */
    public List<MediaChapterBean> _queryDownloadTaskBean_MediaChapterList(String taskName) {
        synchronized (this) {
            if (downloadTaskBean_MediaChapterListQuery == null) {
                QueryBuilder<MediaChapterBean> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.TaskName.eq(null));
                downloadTaskBean_MediaChapterListQuery = queryBuilder.build();
            }
        }
        Query<MediaChapterBean> query = downloadTaskBean_MediaChapterListQuery.forCurrentThread();
        query.setParameter(0, taskName);
        return query.list();
    }

}
